<?php

namespace App\Admin\Controllers;

use App\UserBalanceLog;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserBalanceLogController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '品鉴金列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserBalanceLog());
        $grid->model()->orderBy('updated_at', 'desc')->orderBy('count', 'desc');

        $grid->filter(function($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 设置datetime类型
            $filter->like('user.name', '用户名称');
            $filter->like('order_no', '订单号');
            $filter->between('created_at', '下单时间')->datetime();
        });

        $grid->column('user.name', '用户名')->display(function($value){
            return $value.'<br/>'.$this->user->phone;
        });
        $grid->column('type_value', '类型');
        $grid->column('created_at', '入账时间');
        $grid->column('title', '名称');
        $grid->column('order_no', '业务单号|支付流水号')->display(function($value){
            return $value.'<br>'.$this->transaction_no;
        });
        $grid->column('type', '收支类型')->display(function($value){
            if( in_array($value, [1, 2, 3, 4, 6]) ){
                return '收入';
            }else{
                return '支出';
            }
        });
        $grid->column('money', '额度');
        $grid->column('balance', '余额');
        $grid->disableActions();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserBalanceLog::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('uid', __('Uid'));
        $show->field('type', __('Type'));
        $show->field('money', __('Money'));
        $show->field('from_order_id', __('From order id'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserBalanceLog());

        $form->number('uid', __('Uid'));
        $form->switch('type', __('Type'))->default(1);
        $form->decimal('money', __('Money'))->default(0.00);
        $form->number('from_order_id', __('From order id'));
        $form->switch('status', __('Status'))->default(1);

        return $form;
    }
}
